import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Part of the wizard-mode steps. Prompts the user for
 * their name and the program title.
 * 
 * @author James Stanier
 */
public class WizTitleGUI implements ActionListener {

	CodeIndexGUI wiz5;

	String outputFileName;
	String outputDir;
	String paperType;
	String variableOutput;
	String title;
	String author;
	int fontSize;
	boolean twoSidedPrinting;
	boolean inDepthSection;
	boolean keywordIndex;
	boolean codeIndex;
	boolean titlePage;

	JFrame window;
	JFrame prev;
	JButton nextButton;
	JButton backButton;
	JTextArea titleEntry;
	JTextArea nameEntry;

	ArrayList<File> inputFiles;

	/**
	 * Constructor.
	 * @param g The previous window, CodeIndexGUI.
	 */
	public WizTitleGUI(CodeIndexGUI g) {
		wiz5 = g;
		inputFiles = wiz5.getInputFiles();
		prev = wiz5.getWindow();
		outputFileName = wiz5.getOutputFileName();
		outputDir = wiz5.getOutputDir();
		paperType = wiz5.getPaperType();
		fontSize = wiz5.getFontSize();
		twoSidedPrinting = wiz5.isTwoSidedPrinting();
		inDepthSection = wiz5.isInDepthSection();
		keywordIndex = wiz5.isKeywordIndex();
		variableOutput = wiz5.getVariableOutput();
		codeIndex = wiz5.isCodeIndex();
		title = "";
		author = "";
		wizTitleWindow();
	}

	/**
	 * Configures and draws the screen.
	 */
	public void wizTitleWindow() {
		window = new JFrame("JavaLiN - Wizard mode");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,450);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);

		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);

		// Heading information
		JPanel progress = new JPanel();
		JPanel header = new JPanel();
		progress.setLayout(new BorderLayout());
		header.setLayout(new BorderLayout());
		JLabel backwards = new JLabel("<HTML><font size=2>&#171; Code index</HTML>");
		progress.add(backwards, BorderLayout.WEST);
		JLabel forwards = new JLabel("<HTML><font size=2> Table of contents &#187;</HTML>");
		progress.add(forwards, BorderLayout.EAST);
		JLabel heading = new JLabel("<HTML><b><font size=5>6. Title and author</b></HTML>");
		heading.setHorizontalAlignment(JLabel.CENTER);
		heading.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		header.add(heading, BorderLayout.CENTER);
		JLabel description = new JLabel("<HTML><font size=3>If you would like a title page to the report " +
		"then please fill in the boxes below. If not, leave them blank.</HTML>");
		description.setBorder(BorderFactory.createEmptyBorder(20,10,10,10));
		header.add(description, BorderLayout.SOUTH);
		top.add(progress, BorderLayout.CENTER);
		top.add(header, BorderLayout.SOUTH);

		// Text boxes
		JPanel boxPanel = new JPanel();
		boxPanel.setLayout(new GridLayout(2,2));
		boxPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		JLabel namePrompt = new JLabel("<HTML> Your name:");
		namePrompt.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		namePrompt.setHorizontalAlignment(JLabel.CENTER);
		boxPanel.add(namePrompt);

		nameEntry = new JTextArea();
		nameEntry.setLineWrap(true);
		nameEntry.setWrapStyleWord(true);
		JScrollPane scrollingName = new JScrollPane(nameEntry);
		//scrollingName.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		boxPanel.add(scrollingName);

		JLabel titlePrompt = new JLabel("<HTML> Program title:");
		titlePrompt.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		titlePrompt.setHorizontalAlignment(JLabel.CENTER);
		boxPanel.add(titlePrompt);

		titleEntry = new JTextArea();
		titleEntry.setLineWrap(true);
		titleEntry.setWrapStyleWord(true);
		JScrollPane scrollingTitle = new JScrollPane(titleEntry);
		//scrollingTitle.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		boxPanel.add(scrollingTitle);

		// Navigation buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Next >>");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);

		window.add("North",top);
		window.add("Center",boxPanel);
		window.add("South",navButtons);
		window.validate();
		window.setVisible(true);
	}

	/**
	 * Action to be taken upon component interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(nextButton)) {
			if(!titleEntry.getText().equals("")) {
				title = titleEntry.getText();
				author = nameEntry.getText();
				titlePage = true;
			}
			window.setVisible(false);
			TocGUI tgui = new TocGUI(this);
		}
		else if(x.equals(backButton)) {
			clearSettings();
			window.setVisible(false);
			prev.setVisible(true);
		}
	}

	/**
	 * Clear settings relevant to this screen.
	 */
	public void clearSettings() {
		author = "";
		title = "";
	}

	/**
	 * Get the current font size.
	 * @return The font size as an integer.
	 */
	public int getFontSize() {
		return fontSize;
	}

	/**
	 * Get the current paper type.
	 * @return The paper type as a String.
	 */
	public String getPaperType() {
		return paperType;
	}

	/**
	 * Get current two-sided printing status.
	 * @return Whether two-sided printing is enabled.
	 */
	public boolean isTwoSidedPrinting() {
		return twoSidedPrinting;
	}

	/**
	 * Get the current window.
	 * @return The current JFrame window.
	 */
	public JFrame getWindow() {
		return window;
	}

	/**
	 * Get the current output directory.
	 * @return The output directory as a String.
	 */
	public String getOutputDir() {
		return outputDir;
	}

	/**
	 * Get the output file name.
	 * @return The output file name as a String.
	 */
	public String getOutputFileName() {
		return outputFileName;
	}

	/**
	 * Get whether an in-depth section has been requested.
	 * @return Whether there will be an in-depth section as a boolean.
	 */
	public boolean isInDepthSection() {
		return inDepthSection;
	}

	/**
	 * Get whether a keyword index has been requested.
	 * @return Whether there will be a keyword index as a boolean.
	 */
	public boolean isKeywordIndex() {
		return keywordIndex;
	}

	/**
	 * Get the type of variable output requested.
	 * @return Variable output status as a String.
	 */
	public String getVariableOutput() {
		return variableOutput;
	}

	/**
	 * Get whether a code index has been requested.
	 * @return Whether there will be a code index as a boolean.
	 */
	public boolean isCodeIndex() {
		return codeIndex;
	}

	/**
	 * Get the input source file references.
	 * @return Input source files as File objects.
	 */
	public ArrayList<File> getInputFiles() {
		return inputFiles;
	}

	/**
	 * Get the current author name.
	 * @return The author as a String.
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * Get the current title.
	 * @return The title as a String.
	 */
	public String getTitle() {
		return title;
	}
	
	/**
	 * Get whether the user has requested a title page.
	 * @return Whether a title page is to be included.
	 */
	public boolean isTitlePage() {
		return titlePage;
	}
}
